﻿using DataServer.Entities;
using DataServer.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace IsPay.api
{
    /// <summary>
    /// calcurate 的摘要说明
    /// </summary>
    public class calcurate : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Session["BatchPage"] = null;
            //这个页面用来创建session
            var cardTypes = context.Request.Params["cardType"];
            var cardNos = context.Request.Params["cardNo"];
            var cardName = context.Request.Params["cardName"];
            var amounts = context.Request.Params["amount"];
            //cardType,cardNo,cardName,amount

            if (cardTypes != null && cardNos != null && cardName != null && amounts != null)
            {
                string[] arrCardType = cardTypes.Split(',');
                string[] arrCardNo = cardNos.Split(',');
                string[] arrCardName = cardName.Split(',');
                string[] arrAmounts = amounts.Split(',');

                int isum = 0;
                List<BatchPay> lstBatchPay = new List<BatchPay>();
                for (int idx = 0; idx < arrCardType.Length; idx++)
                {
                    isum += int.Parse(arrAmounts[idx]);

                    BatchPay batchPay = new BatchPay();
                    batchPay.BankCardType = arrCardType[idx];
                    batchPay.BankCardNo = arrCardNo[idx];
                    batchPay.CardName = arrCardName[idx];
                    batchPay.Amount = arrAmounts[idx];

                    lstBatchPay.Add(batchPay);
                }

                BatchPage batchPage = new BatchPage();
                batchPage.OrderCount = cardTypes.Length;
                batchPage.OrderCount = isum;
                batchPage.LstBatchPay = lstBatchPay;

                context.Session["BatchPage"] = batchPage;

                context.Response.ContentType = "text/plain";
                context.Response.Write(MakeWebReturnCode.MakeCode(Constants.WebRetOK));
            }
            else
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write(MakeWebReturnCode.MakeCode(Constants.WebRetNotDataIn));
            }          
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}